package com.youli.gb.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.youli.gb.constant.SysConstatnt;
import com.youli.gb.model.SysUser;
/**
 * 权限过滤器
 * @author sjxy
 *  1·用户管理
 *    1）要求登录：增加，删除，修改，重置密码，修改密码，注销
 *    2）不用登录：登录
 */
public class AuthorityFilter implements Filter {

	
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request =((HttpServletRequest)req);
		HttpServletResponse response =((HttpServletResponse)resp);
		HttpSession session=request.getSession();
		SysUser loginUser=(SysUser)session.getAttribute(SysConstatnt.SESSION_USER);
		
		String uri = request.getRequestURI();
		StringBuffer url = request.getRequestURL();
		String qs = request.getQueryString();
		String servletPath = request.getServletPath();
		String contextPath = request.getContextPath();
		
		url.toString().endsWith("css");
		
		String path =servletPath + "?" + qs;
		
		System.out.println("uri=======" + uri);
		System.out.println("url=======" + url.toString());
		System.out.println("qs=======" + qs);
		System.out.println("servletPath=======" + servletPath);
		System.out.println("contextPath=======" + contextPath);
		
		if(loginUser !=null 
				|| path.contains("user.do?type=login") 
				|| path.contains("user.do?type=zan") 
				|| path.contains("user.do?type=list")
				|| path.contains("index")
				|| uri.endsWith("css")
				|| uri.endsWith("js")) {
			System.out.println("放行");
			chain.doFilter(request,response);
		}
		else {
			System.out.println("拦截");
			response.sendRedirect(contextPath+"/index");
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}

}
